package test.transferFile.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import test.transferFile.codec.TransferFileCodecFactory;
import test.transferFile.thread.TransferFileWorkThread;

public class MinaFileClient {

  private NioSocketConnector connector;

  public void startClient() {
    connector=new NioSocketConnector();
    DefaultIoFilterChainBuilder chain=connector.getFilterChain();
    chain.addLast("transferFileFilter", new ProtocolCodecFilter(new TransferFileCodecFactory(Charset.forName("utf-8"))));
    connector.setHandler(new MinaFileClientHandler());
    ConnectFuture cf=connector.connect(new InetSocketAddress("localhost", 9999));
    cf.awaitUninterruptibly();
    IoSession session=cf.getSession();
    String fileName="货币战争.pdf";
    try {
      File file=new File("e:\\" + fileName);
      FileInputStream fis=new FileInputStream(file);
      new TransferFileWorkThread(fis, session, fileName).start();
    } catch(FileNotFoundException e) {
      e.printStackTrace();
    }
  }

  public void sendFile(String filePath) throws FileNotFoundException {
  }

  public static void main(String[] args) {
    new MinaFileClient().startClient();
  }
}
